package com.xhtt.modules.inspection.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.Query;
import com.xhtt.modules.inspection.dao.InsPointLabelMapper;
import com.xhtt.modules.inspection.dto.PointLabelDTO;
import com.xhtt.modules.inspection.entity.InsLabel;
import com.xhtt.modules.inspection.entity.InsPoint;
import com.xhtt.modules.inspection.entity.InsPointLabel;
import com.xhtt.modules.inspection.service.IInsLabelService;
import com.xhtt.modules.inspection.service.IInsPointLabelService;
import com.xhtt.modules.inspection.service.IInsPointService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签与检点的关系表 服务实现类
 * </p>
 *
 * @author xj
 * @since 2019-01-18
 */
@Service
@Slf4j
public class InsPointLabelServiceImpl extends ServiceImpl<InsPointLabelMapper, InsPointLabel> implements IInsPointLabelService {

    @Autowired
    private IInsPointService insPointService;

    @Autowired
    private IInsLabelService insLabelService;

    @Override
    public List<String> getDeviceNamesByLabelId(Integer labelId) {
        List<InsPointLabel> insPointLabel = baseMapper
                .selectList(new LambdaQueryWrapper<InsPointLabel>()
                        .eq(InsPointLabel::getLabelId, labelId));

        // 如果有关联
        if (CollectionUtils.isNotEmpty(insPointLabel)) {
            List<Integer> pointIdList = insPointLabel.stream()
                    .filter(e -> e.getLabelId().equals(labelId))
                    .map(InsPointLabel::getPointId)
                    .collect(Collectors.toList());
            return insPointService.listByIds(pointIdList).stream().map(InsPoint::getDeviceName).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public String getLabelNameByPointId(Integer pointId) {
        InsLabel insLabel = this.getInsLabelByPointId(pointId);
        if (insLabel != null) {
            return insLabel.getName();
        }
        return null;
    }

    @Override
    public InsLabel getInsLabelByPointId(Integer pointId) {
        InsPointLabel insPointLabel = baseMapper.selectOne(new LambdaQueryWrapper<InsPointLabel>().eq(InsPointLabel::getPointId, pointId));
        if (insPointLabel != null) {
            Integer labelId = insPointLabel.getLabelId();
            InsLabel insLabel = insLabelService.getOne(new LambdaQueryWrapper<InsLabel>().eq(InsLabel::getId, labelId));
            if (insLabel != null) {
                return insLabel;
            }
        }
        return null;
    }

    @Override
    public PageUtils getPointLabelList(Map<String, Object> params) {

        Page<InsPointLabel> page = new Query<InsPointLabel>(params).getPage();

        IPage<InsPointLabel> iPage = baseMapper.selectPage(page, new LambdaQueryWrapper<>());

        /*
         * 因为检测点对应一个标签
         * 关联表只会有一个关联关系
         * 直接查询所有关联表数据就可以获得所有的检测点以及对应的标签名称
         */

        List<InsPointLabel> insPointLabelList = iPage.getRecords();

        // 构造返回集合
        List pointLabelDTOList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(insPointLabelList)) {
            for (InsPointLabel insPointLabel : insPointLabelList) {

                // 检测点数据
                InsPoint insPoint = insPointService.getById(insPointLabel.getPointId());

                // 标签数据
                InsLabel insLabel = insLabelService.getById(insPointLabel.getLabelId());


                // 如果检测点的数据不存在
                if (insPoint != null) {

                    // 构造返回对象
                    PointLabelDTO pointLabelDTO = new PointLabelDTO();

                    pointLabelDTO.setId(insPoint.getId());
                    pointLabelDTO.setName(insPoint.getName());
                    pointLabelDTO.setDeviceName(insPoint.getDeviceName());
                    pointLabelDTO.setDeviceType(insPoint.getDeviceType());

                    if (insLabel != null) {
                        pointLabelDTO.setLabelName(insLabel.getName());
                    } else {
                        log.error("该检测点未绑定标签 检测点id {}", insPoint.getId());
                    }
                    pointLabelDTOList.add(pointLabelDTO);
                }
            }
        }
        iPage.setRecords(pointLabelDTOList);
        return new PageUtils(iPage);
    }

}
